﻿using Sayook.Framework.DbCore.SqlModel;
using System;
using System.Linq.Expressions;

namespace Sayook.Framework.DbCore.CreateSqlString
{
    /// <summary>
    /// 接口：将表达式转化为SQL字句
    /// </summary>
    public interface ICreateSql
    {
        /// <summary>
        /// 生成 SELECT 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类</typeparam>
        /// <returns>"SELECT * FROM 表名称"</returns>
        string CreateSelect<T>() where T : DbModelBase;

        /// <summary>
        /// 生成 SELECT 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类</typeparam>
        /// <param name="expression">查询字段表达式 eg:x => new { x.Id, x.Name, ...}</param>
        /// <returns>"SELECT field1,field2... FROM 表名称"</returns>
        string CreateSelect<T>(Expression<Func<T, object>> expression) where T : DbModelBase;

        /// <summary>
        ///  生成 SELECT DISTINCT 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <typeparam name="TElement">查询字段的类型</typeparam>
        /// <param name="expression">查询字段表达式 eg:x => x.Name</param>
        /// <returns>"SELECT DISTINCT 字段名 FROM 表名称"</returns>
        string CreateSelectDistinct<T, TElement>(Expression<Func<T, TElement>> expression) where T : DbModelBase;

        /// <summary>
        /// 生成 WHERE 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类</typeparam>
        /// <param name="expression">条件字段表达式 eg:x => SqlWhere.Where(x.Id, SqlOperator.Equal)</param>
        /// <returns>"WHERE field = @field"</returns>
        string CreateWhere<T>(Expression<Func<T, SqlWhere>> expression);

        /// <summary>
        /// 生成 WHERE 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类</typeparam>
        /// <typeparam name="TElement">查询字段的类型</typeparam>
        /// <param name="expression">条件字段表达式 eg:x => SqlWhere.Where(x.Id, SqlOperator.Equal)</param>
        /// <returns>"WHERE field = @field"</returns>
        string CreateWhere<T, TElement>(Expression<Func<T, TElement>> expression);

        /// <summary>
        /// 生成 WHERE 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类</typeparam>
        /// <typeparam name="TElement">查询字段的类型</typeparam>
        /// <typeparam name="TValue">参数类型</typeparam>
        /// <param name="expression">条件字段表达式 eg:x => SqlWhere.Where(x.Id, SqlOperator.Equal)</param>
        /// <param name="value">条件字段的值</param>
        /// <returns>"WHERE field = value"</returns>
        string CreateWhere<T, TElement, TValue>(Expression<Func<T, TElement>> expression, TValue value);

        /// <summary>
        /// 生成 AND 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类</typeparam>
        /// <param name="expression">条件字段表达式 eg:x => SqlWhere.Where(x.Name, SqlOperator.Unqual)</param>
        /// <returns>"AND field <> @field"</returns>
        string CreateAnd<T>(Expression<Func<T, SqlWhere>> expression);

        /// <summary>
        /// 生成 OR 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类</typeparam>
        /// <param name="expression">条件字段表达式 eg:x => SqlWhere.Where(x.Id, SqlOperator.Less)</param>
        /// <returns>"OR field < @field"</returns>
        string CreateOr<T>(Expression<Func<T, SqlWhere>> expression);

        /// <summary>
        /// 生成 ORDER BY 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <typeparam name="TElement">排序字段类型</typeparam>
        /// <param name="expression">ORDER BY表达式 eg:x => x.Id</param>
        /// <returns>"field ASC"</returns>
        string CreateOrderBy<T, TElement>(Expression<Func<T, TElement>> expression);

        /// <summary>
        /// 生成 ORDER BY 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <typeparam name="TElement">排序字段类型</typeparam>
        /// <param name="expression">ORDER BY表达式 eg:x => x.Id</param>
        /// <returns>"field DESC"</returns>
        string CreateOrderByDesc<T, TElement>(Expression<Func<T, TElement>> expression);

        /// <summary>
        /// 生成 INSERT 语句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <typeparam name="TElement">更新字段的类型</typeparam>
        /// <param name="expression">新增记录的表达式:x => new { x.Id, x.Name }</param>
        /// <returns></returns>
        string CreateInsert<T, TElement>(Expression<Func<T, TElement>> expression) where T : DbModelBase;

        /// <summary>
        /// 生成 UPDATE 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <typeparam name="TElement">更新字段的类型</typeparam>
        /// <param name="expression">更新字段的表达式 eg:x => new { x.Name, x.Remark }</param>
        /// <returns> "UPDATE 表名称 SET Name = @name,Remark = @Remark"</returns>
        string CreateUpdate<T, TElement>(Expression<Func<T, TElement>> expression) where T : DbModelBase;

        /// <summary>
        /// 生成 DELETE 语句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <returns>"DELETE FROM 表名称"</returns>
        string CreateDelete<T>() where T : DbModelBase;

        /// <summary>
        /// 生成 指定读取行数 字句
        /// </summary>
        /// <param name="skipCount">跳过的行数</param>
        /// <param name="takeCount">读取的行数</param>
        /// <returns></returns>
        string CreatePageing(int skipCount, int takeCount);
    }
}